import commonConstants from "@/common/commonConstants";
import commonMethod from "@/common/commonMethod";
import { CallbackProperty, ClockRange, Ellipsoid, JulianDate, SampledPositionProperty, TimeInterval, TimeIntervalCollection, Transforms, VelocityOrientationProperty, VelocityVectorProperty } from "cesium";
import $ from "jquery"
/** 
 * 通过不同的参数，获得不同的推演引擎
 */


class DeduceEngine{

    constructor(viewer,op){
        this.curViewer = viewer;
        this.isGC = false;
        this.isEngineActive = false;
        

        this.scenarioScript = undefined;
        this.deduceBeforeTime =undefined;
        if (op){
            if (op["beforePlayEvent"]){
                this.beforePlayEvent = op["beforePlayEvent"];
            }
            if (op["stopPlayEvent"]){
                this.stopPlayEvent = op["stopPlayEvent"];
            }
        }

        this.pauseBeforeExecuteEvent =()=>{
            this.parse();
            this.isGC = false;
        };

        this.playBeforeExecuteEvent =()=>{
            this.start();
            this.isGC = false;
        };

        this.stopExecuteEvent = () =>{
            if (this.isGC){
                return;
            }
            this.stop();
            this.isGC = true;
            this.isEngineActive = false;
        }
    }
    setBeforeFunction(beforefn){
        let _this =this;
        _this.beforePlayEvent = beforefn;
    }
    setStopFunction(stopPlayfn){
        let _this= this;
        _this.stopPlayEvent = stopPlayfn;
    }
    getEngineActive(){
        let _this = this;
        return _this.isEngineActive;
    }
    /**
     *
     * @param scenarioScript {ScenarioScript}
     */
    loadScript(scenarioScript){
        let _this =this;
        _this.scenarioScript = scenarioScript;

        //记录住当前的时间
        _this.deduceBeforeTime = {
            start :_this.curViewer.clock.startTime ,
            curTime:_this.curViewer.clock.currentTime,
            stopTime:_this.curViewer.clock.stopTime
        };

        let bool = _this.scenarioScript.prepare();
        if (bool){
            //提示信息
            // commonMethod.showMsg(bool
            return;
        }

        //将一切的事件都加入到控制器中
        let animationModel= _this.curViewer.animation.viewModel;
        animationModel.pauseViewModel.command.beforeExecute.addEventListener(_this.pauseBeforeExecuteEvent);
        animationModel.playForwardViewModel.command.beforeExecute.addEventListener(_this.playBeforeExecuteEvent);

        _this.curViewer.clock.onStop.addEventListener(_this.stopExecuteEvent);
    }
    changeSVG(num){
        let _this = this;
        //中间播放的按钮元素
        let one = $(".cesium-animation-buttonPath");
        one = $(one[3]);

        //动画控制器的对象
        let animationModel= _this.curViewer.animation.viewModel;
        let bool = animationModel.pauseViewModel.toggled;
        let bool2 = animationModel.playForwardViewModel.toggled;

        if(!bool ){
            if(!bool2){
                one.attr("href","#animation_pathPause");
            }
            if(num === 1){
                one.attr("href", "#animation_pathPlay")
            }
        }else{
            one.attr("href","#animation_pathPause");
        }
    }
    scenarioOnTime() {
        let _this =this;
        let currentTime = _this.curViewer.clock.currentTime;
        if (JulianDate.equals(currentTime, _this.curViewer.clock.startTime)) {
            //播放开始前，进行脚本绑定
            if (_this.scenarioScript.isPrepareValue()) {
                let bool = _this.scenarioScript.flushScript();
                if (bool){
                    // CommonMethod.showMsg(bool);
                    _this.curViewer.clock.currentTime = _this.curViewer.clock.stopTime;
                    return
                }
            }else {
                let bool = _this.scenarioScript.prepare();
                if (bool){
                    // CommonMethod.showMsg(bool);
                    _this.curViewer.clock.currentTime = _this.curViewer.clock.stopTime;
                    return;
                }
            }
            if (_this.beforePlayEvent) {
                _this.beforePlayEvent();
            }
            _this.isEngineActive = true;
        }else if (JulianDate.equals(currentTime,_this.curViewer.clock.stopTime)){
            _this.curViewer.clock.currentTime = _this.curViewer.clock.startTime;
        }
    }
    start () {
        let _this=this;
        _this.changeSVG(2);

        _this.scenarioOnTime();
    }
    parse() {
        let _this=this;
        _this.changeSVG(1);

        _this.scenarioOnTime();
    }
    replay () {

    }
    stop () {
        let _this =this;
        _this.curViewer.clock.currentTime=_this.curViewer.clock.startTime;

        if(_this.stopPlayEvent){
            _this.stopPlayEvent();
        }
        _this.scenarioScript.gc();
    }
    wittiness (actor) {
        let _this =this;
        _this.scenarioScript.addActor(actor);
        _this.scenarioScript.flushScript();
    }
    /**
     * 引擎清空，当前想定相关完全消除
     */
    engineClear() {
        let _this = this;
        _this.curViewer.shouldAnimate = false;
        _this.curViewer.clock.startTime  = _this.deduceBeforeTime.start;
        _this.curViewer.clock.currentTime = _this.deduceBeforeTime.curTime;
        _this.curViewer.clock.stopTime = _this.deduceBeforeTime.stopTime;
        //一些事件去除
        const animationModel= _this.curViewer.animation.viewModel;
        animationModel.pauseViewModel.command.beforeExecute.removeEventListener(_this.pauseBeforeExecuteEvent);
        animationModel.playForwardViewModel.command.beforeExecute.removeEventListener(_this.playBeforeExecuteEvent);

        _this.curViewer.clock.onStop.removeEventListener(_this.stopExecuteEvent);
        //对所有的标绘和兵力和航迹同时进行删除
        _this.scenarioScript.destroy();
    }
}



class ActorFactory{
    constructor(v,configs){

       this.actosMap = new Map();
       this.curViewer = v;
    }

    /**
     * 通过Key获得效果,工厂
     * @param regKey 效果注册的key
     * @param param 效果的参数设置
     */
    buildActor(actorKey,param){
        let actor;
        
        if(actorKey == commonConstants.actorType.move){
            actor = new MovingActor(this.curViewer,param);
        }else if(actorKey == commonConstants.actorType.decision){
            actor = new DecisionActor(this.curViewer,param);
        }else if(actorKey == commonConstants.actorType.Influence){
            actor = new InfluenceActor(this.curViewer,param)
        }

        this.actosMap.set(actor.getID(),actor);
        return actor;
    }
    destoryActor(actor){
        actor.destory();
        this.actosMap.delete(actor.getID())
    }
}



class ScenarioScript{

    constructor(viewer,configs){
        this.actors = [];

        this.curViewer = viewer;
        this.actorHandler = new ActorFactory(viewer,configs);
        this.isPrepare =false;
        this.scenarioMultiplier = 10;

        this.scenarioStart = undefined;
        this.scenarioEnd = undefined;
    }

    getActorById(actorId){
        let _this = this;
        let findActor;
        for (let i = 0; i < _this.actors.length; i++) {
            let actor = _this.actors[i];
            if (actor.getActorId() === actorId){
                findActor = actor;
                break;
            }
        }
        return findActor;
    }
    getEndJulianData(){
        let _this = this;
        return _this.scenarioEnd;
    }
    getStartJulianDate(){
        const _this = this;
        return _this.scenarioStart;
    }
    getStartScenarioTimeString(){
        let _this =this;
        const data = JulianDate.toDate(_this.scenarioStart);
        return commonMethod.dateFormat(data);
    }
    getEndScenarioTimeString(){
        let _this =this;
        const d = JulianDate.toDate(_this.scenarioEnd);
        return commonMethod.dateFormat(d);
    }

    /**
     * 计算时间长度，根据演员计算
     * @param actor 
     */
    _costTimes(actor){
        const _this = this;
        const endTime = actor.getEndJulianData();
        const startTime = actor.getStartJulianDate();
        if (_this.scenarioEnd && endTime) {
            if(JulianDate.greaterThan(endTime,this.scenarioEnd)){
               _this.scenarioEnd = endTime
            }
        }else{
            _this.scenarioEnd = endTime;
        }
        
        if (_this.scenarioStart && startTime) {
            if(JulianDate.greaterThan(_this.scenarioStart,startTime)){
                _this.scenarioStart = startTime
             }
        } else {
            _this.scenarioStart = startTime;
        }
    }
    createActor(actorType,options){
        let _this = this;
        let actor = _this.actorHandler.buildActor(actorType,options);
        actor.setScenarioScript(_this);
        _this.addActor(actor);
        return  actor;
    }
    /**
     *开始和结束时间，动态计算，没有设置
     * @param actor {Actor}
     */
    addActor(actor){
        let _this =this;
        if (_this.actors.indexOf(actor) === -1){
            _this.actors.push(actor);

            _this._costTimes(actor)
        }
    }
    removeActor (actor) {
        let _this = this;
        let index = _this.actors.indexOf(actor);
        if (index !== -1){
            if (actor.isPrepareValue()){
                actor.gc();
            }
            _this.actors.splice(index,1);
        }
    }
    /**
     *
     * @return {null|[]|(Actor)[]}
     */
    getAllActors(){
        let _this =this;
        return _this.actors;
    }
    flushScript(){
        let _this = this;
        if (_this.actors.length === 0){
            return "没有参与推演的元素，无法进行推演！";
        }
        //演员就位
        for (let i = 0; i < _this.actors.length; i++) {
            let actor = _this.actors[i];
            if (!actor.isPrepareValue()){
               let bool =  actor.prepare();
               if (bool){
                   return bool;
               }
            }
        }
    }
    prepare () {
        let _this =this;
        //时间处理
        //修改时钟属性，并添加实例到 viewer,可以在推演是的准备中进行设置
        if(!_this.scenarioStart.equals(_this.curViewer.clock.startTime)){
            _this.curViewer.clock.startTime=_this.scenarioStart.clone();
            _this.curViewer.clock.currentTime=_this.scenarioStart.clone();
        }

        if(!_this.scenarioEnd.equals(_this.curViewer.clock.stopTime)){
            _this.curViewer.clock.stopTime=_this.scenarioEnd.clone();
        }

        _this.curViewer.clock.multiplier = _this.scenarioMultiplier;
        _this.curViewer.clock.clockRange = ClockRange.CLAMPED;

        //设置时间线时间范围
        _this.curViewer.timeline.zoomTo(_this.scenarioStart,_this.scenarioEnd);
        _this.curViewer.timeline.resize();
        _this.curViewer.timeline.container.style.display="block";
        _this.curViewer.animation.container.style.display="block";
        //演员就位
        for (let i = 0; i < _this.actors.length; i++) {
            let actor = _this.actors[i];
            if (!actor.isPrepareValue()){
                let bool =  actor.prepare();
                if (bool){
                    return bool;
                }
            }
        }
        _this.isPrepare = true;
    }
    isPrepareValue(){
        let _this =this;
        return _this.isPrepare;
    }
    gc () {
        let _this = this;
        for (let i = 0; i < _this.actors.length; i++) {
            _this.actors[i].gc();
        }
        _this.isPrepare = false;
    }
    destroy () {
        let _this = this;
        _this.actors= [];
        _this.isPrepare = false;
    }
    reload () {
        let _this = this;
        // const actorResult = _this.dataStorage.load(CommonConstants.dataOperationType.actor);
        // if (actorResult){
        //     let actorJsons = actorResult["actors"];
        //     for (let i = 0; i < actorJsons.length; i++) {
        //         if (actorJsons[i]["actorType"] === "request"){
        //             continue;
        //         }
        //         _this.createActor(actorJsons[i]["actorType"],actorJsons[i]);
        //     }
        // }
    }

}

/**
 * 使兵力按着航迹进行移动
 */
class MovingActor{
    
    constructor(v,param){

        this.actorId = commonMethod.creatUUID();
        this.actorName = undefined;
        this.actorType = commonConstants.actorType.move;

        this.startTimeString = undefined;
        this.startTimeJulianData = undefined;
        this.startTimeData = undefined;
        this.endTimeString = undefined;
        this.endTimeJulianData= undefined;
        this.endTimeData= undefined;
        this.actorDescription= undefined;
        this.scenarioScript = undefined;

        this.materials= {
            troop:undefined,
            track: undefined
        };
        //是否触发完成
        this.isTriggered= false;
        this.curViewer= v;
        this.isPrepare= false;
        this.times= [];

        this._handlerOptions(param)
    }


    _handlerOptions(options){
        const _this = this;
        if (options){
            if (options["actorId"]){
                _this.actorId = options["actorId"];
            }
            if (options["actorName"]){
                _this.actorName = options["actorName"];
            }
            if (options["actorDescription"]){
                _this.actorDescription = options["actorDescription"]
            }
            if (options["actorDesc"]){
                _this.actorDescription = options["actorDesc"];
            }
            if (options["startTime"]){
                _this.setStartTimeString(options["startTime"]);
            }
            if (options["endTime"]){
                _this.setEndTimeString(options["endTime"]);
            }
            if (options["actorMaterials"]){
                let actorMaterialsJson = options["actorMaterials"];
                _this.materials.troop = actorMaterialsJson["troop"];

                _this.materials.track = actorMaterialsJson["track"];
            }
            if (options["troop"]){
                _this.materials.troop = options["troop"];
            }
            if (options["track"]){
                _this.materials.track = options["track"];
            }
        }
    }
    /**
     *
     * @param scenarioScript {ScenarioScript}
     */
    setScenarioScript(scenarioScript){
        let _this =this;
        _this.scenarioScript = scenarioScript;
    }
    
    getID(){
        return this.actorId;
    }
    getActorId(){
        let _this = this;
        return _this.actorId;
    }
    getActorType(){
        let _this =this;
        return _this.actorType;
    }
    isPrepareValue(){
        let _this =this;
        return _this.isPrepare;
    }
    setStartTimeString(startTimeString){
        let _this =this;
        _this.startTimeString = startTimeString;
        _this.startTimeData = new Date(startTimeString);
        _this.startTimeJulianData = JulianDate.fromDate(_this.startTimeData);
    }
    setEndTimeString(endTimeString){
        let _this =this;
        _this.endTimeString = endTimeString;
        _this.endTimeData = new Date(endTimeString);
        _this.endTimeJulianData = JulianDate.fromDate(_this.endTimeData);
    }
    getStartTimeString(){
        let _this = this;
        return _this.startTimeString;
    }
    getEndTimeString(){
        let _this = this;
        return _this.endTimeString;
    }
    getEndJulianData(){
        let _this = this;
        return _this.endTimeJulianData;
    }
    getStartJulianDate(){
        let _this = this;
        return _this.startTimeJulianData;
    }
    getActorName(){
        let _this = this;
        return _this.actorName;
    }
    getMaterials(){
        let _this = this;
        return _this.materials;
    }
    prepare () {
        let _this = this;
        //进行绑定
        let troop = _this.materials.troop;
        let troopBool = troop.getIsDeploy();
        if (!troopBool){
            return "兵力没有部署！";
        }
        let trackBool = _this.materials.track.getEntity();
        if (!trackBool){
            return "航迹没有实现！";
        }

        //对时间进行测量
        let start = _this.startTimeJulianData;
        let end = _this.endTimeJulianData;
        let endTime=_this.scenarioScript.getEndJulianData();
        let property = _this._getMoviongPositions(start,end,endTime);

        let entity = troop.getEntity();
        entity.availability = new TimeIntervalCollection([new TimeInterval({
            start : start,
            stop : endTime
        })]);
        entity.position=property;
        entity.orientation=new VelocityOrientationProperty(property);
        if (entity.billboard){
            entity.billboard.alignedAxis=new VelocityVectorProperty(property,true);
        };
        //对于状态的改变进行效果判断
        _this.curViewer.clock.onTick.addEventListener((clock) =>{
            if (_this.isTriggered){
                return;
            }
            let currentTime = clock.currentTime;
            if(JulianDate.lessThan(end,currentTime)){
                if(troop.getState() === commonConstants.troopState.Normal){
                    troop.setShow(true);
                }else if(troop.getState() === commonConstants.troopState.Crash || troop.getState() === commonConstants.troopState.Hiding){
                    troop.setShow(false);
                }
                _this.isTriggered = true;
            }
        });
        if(troopBool){
            troop.prepare();
        }
        if (trackBool){
            _this.materials.track.prepare();
        }
        console.log("准备就绪："+_this.actorName);
        _this.isPrepare = true;
    }
    gc(){
        let _this = this;
        _this.isTriggered = false;
        _this.isPrepare = false;
        _this.times = [];
        _this.materials.troop.gc();
        _this.materials.track.gc();
    }
    _getMoviongPositions(start,end,scenarioEnd){
        let _this=this;
        let property = new SampledPositionProperty();
        //计算时间间隔
        _this._costTimes(start,end);

        let track = _this.materials.track;

        //获得新的时间并进行插值

        const length = _this.times.length;
        let endPosition = track.getPositions()[length-1];
        for(let j=0; j<length; j++){
            //不进行转换直接赋值可行？
            let troopPosition=track.getPositions()[j];
            property.addSample(_this.times[j],troopPosition);
        }

        //如果是没有到最晚，就加上最后一个点
        if(JulianDate.greaterThan(scenarioEnd,end)){
            property.addSample(scenarioEnd,endPosition);
        }
        return property;
    }
    _costTimes (start,end) {
        let _this =this;
        let track = _this.materials.track;
      
        if(track.length === 2){
            _this.times.push(start,end);
        }else{
            let time=JulianDate.toDate(end).getTime() - JulianDate.toDate(start).getTime();
            let mm=time/(track.getPositions().length-1);
            for(let j=0;j<track.getPositions().length;j++){
                _this.times.push(JulianDate.addSeconds(start,mm*0.001*j,new JulianDate()))
            }
        }
        
    }
    toJson () {
        let _this =this;

        let materialJson = {
            troop: _this.materials.troop.getTroopId(),
            track:_this.materials.track.getTrackId()
        };
        return{
            actorId:_this.actorId,
            actorName:_this.actorName,
            actorType:_this.actorType,
            startTime:_this.startTimeString,
            endTime:_this.endTimeString,
            actorDesc:_this.actorDescription?_this.actorDescription:"",
            actorMaterials:materialJson,
            scenarioId:scenario_deduce.getCurScenario().getScenarioId()
        }
    }
}


/**
 * 兵力决策演员
 */
class DecisionActor{
    constructor(v,param){
    
        this.actorId = commonMethod.creatUUID();
        this.actorName = undefined;
        this.actorType = commonConstants.actorType.decision;

        this.startTimeString = undefined;
        this.startTimeJulianData = undefined;
        this.startTimeData = undefined;
        this.endTimeString = undefined;
        this.endTimeJulianData= undefined;
        this.endTimeData= undefined;
        this.actorDescription= undefined;
        this.scenarioScript = undefined;

        this.troop = undefined;
        this.decisionlink = undefined;
        //是否触发完成
        this.isTriggered= false;
        this.curViewer= v;
        this.curScenario = undefined;
        this.isPrepare= false;
        this.lastTime = undefined;
        this.p = undefined;

        this._handlerOptions(param)
    }


    _handlerOptions(options){
        const _this = this;
        if (options){
            if (options["actorId"]){
                _this.actorId = options["actorId"];
            }
            if (options["actorName"]){
                _this.actorName = options["actorName"];
            }
            if (options["actorDescription"]){
                _this.actorDescription = options["actorDescription"]
            }
            if (options["actorDesc"]){
                _this.actorDescription = options["actorDesc"];
            }
            
            if (options["curScenario"]){
                _this.curScenario = options["curScenario"];
            }
            if (options["troop"]){
                _this.troop = options["troop"];
            }
            if (options["decisionlink"]){
                _this.decisionlink = options["decisionlink"];
            }
            if (options["startTime"]){
                _this.setStartTimeString(options["startTime"]);
            }
            if (options["endTime"]){
                _this.setEndTimeString(options["endTime"]);
            }
        }
    }
  /**
     *
     * @param scenarioScript {ScenarioScript}
     */
  setScenarioScript(scenarioScript){
    let _this =this;
    _this.scenarioScript = scenarioScript;
    }

    getID(){
        return this.actorId;
    }
    getActorId(){
        let _this = this;
        return _this.actorId;
    }
    getActorType(){
        let _this =this;
        return _this.actorType;
    }
    isPrepareValue(){
        let _this =this;
        return _this.isPrepare;
    }
    setStartTimeString(startTimeString){
        let _this =this;
        _this.startTimeString = startTimeString;
        _this.startTimeData = new Date(startTimeString);
        _this.startTimeJulianData = JulianDate.fromDate(_this.startTimeData);
    }
    setEndTimeString(endTimeString){
        let _this =this;
        _this.endTimeString = endTimeString;
        _this.endTimeData = new Date(endTimeString);
        _this.endTimeJulianData = JulianDate.fromDate(_this.endTimeData);
    }
    getStartTimeString(){
        let _this = this;
        return _this.startTimeString;
    }
    getEndTimeString(){
        let _this = this;
        return _this.endTimeString;
    }
    getEndJulianData(){
        let _this = this;
        return _this.endTimeJulianData;
    }
    getStartJulianDate(){
        let _this = this;
        return _this.startTimeJulianData;
    }
    getActorName(){
        let _this = this;
        return _this.actorName;
    }
    prepare () {
        let _this = this;
        //进行绑定
        let troopBool = _this.troop.getIsDeploy();
        if (!troopBool){
            return "兵力没有部署！";
        }

        //对时间进行测量
        let start = _this.startTimeJulianData;
        let end = _this.endTimeJulianData;
        let endTime=_this.scenarioScript.getEndJulianData();
        let property = _this._getMoviongPositions(start);

        let entity = _this.troop.getEntity();
        entity.availability = new TimeIntervalCollection([new TimeInterval({
            start : start,
            stop : endTime
        })]);
        entity.position=property;
        //对于状态的改变进行效果判断
        _this.curViewer.clock.onTick.addEventListener((clock) =>{
            if (_this.isTriggered) {
                return
            }
            let currentTime = clock.currentTime;
            if (_this.decisionlink && !_this.decisionlink.getDone()) {
                _this.decisionlink.decisionProcessing(_this.curScenario,currentTime)
            }

            if(JulianDate.lessThan(end,currentTime)){
                if(_this.troop.getState() === commonConstants.troopState.Normal){
                    _this.troop.setShow(true);
                }else if(_this.troop.getState() === commonConstants.troopState.Crash || _this.troop.getState() === commonConstants.troopState.Hiding){
                    _this.troop.setShow(false);
                }
                _this.isTriggered = true;
            }
        });
        if(troopBool){
            _this.troop.prepare();
        }
        console.log("准备就绪："+_this.actorName);
        _this.isPrepare = true;
    }
    gc(){
        let _this = this;
        _this.isTriggered = false;
        _this.isPrepare = false;
        _this.times = [];
        _this.materials.troop.gc();
        _this.materials.track.gc();
    }
    _getMoviongPositions(start){
        let _this=this;
        if(!_this.lastTime){
            _this.lastTime = start;
            _this.p = _this.troop.getStaticPosition();
        }
        let property = new CallbackProperty(time=>{
           
            const t = JulianDate.secondsDifference(time,_this.lastTime)
            if(t<=0){
                return _this.p;
            }
            const v = _this.troop.speed
            const h = _this.troop.heading;
      //根据速度和方向，可以计算下一个点
            const pos = commonMethod.calculateNextPosition(_this.p,t,v,h)

            _this.lastTime = time;
            _this.p =  pos?pos:p;
          
           return _this.p;

        },false);
        return property;
    }
    toJson () {
        let _this =this;

        let materialJson = {
            troop: _this.materials.troop.getTroopId(),
            track:_this.materials.track.getTrackId()
        };
        return{
            actorId:_this.actorId,
            actorName:_this.actorName,
            actorType:_this.actorType,
            startTime:_this.startTimeString,
            endTime:_this.endTimeString,
            actorDesc:_this.actorDescription?_this.actorDescription:"",
            actorMaterials:materialJson,
            scenarioId:scenario_deduce.getCurScenario().getScenarioId()
        }
    }

}

/**
 * 影响演员
 */
class InfluenceActor{
    constructor(v,param){
        this.actorId = commonMethod.creatUUID();
        this.actorName = undefined;
        this.actorType = commonConstants.actorType.Influence;

        this.startTimeString = undefined;
        this.startTimeJulianData = undefined;
        this.startTimeData = undefined;
        this.endTimeString = undefined;
        this.endTimeJulianData= undefined;
        this.endTimeData= undefined;
        this.actorDescription= undefined;
        this.scenarioScript = undefined;

        this.influence = undefined;
        this.curScenario = undefined;
        //是否触发完成
        this.isTriggered= false;
        this.curViewer= v;
        this.isPrepare= false;

        this._handlerOptions(param)
    }


    _handlerOptions(options){
        const _this = this;
        if (options){
            if (options["actorId"]){
                _this.actorId = options["actorId"];
            }
            if (options["actorName"]){
                _this.actorName = options["actorName"];
            }
            if (options["actorDescription"]){
                _this.actorDescription = options["actorDescription"]
            }
            if (options["actorDesc"]){
                _this.actorDescription = options["actorDesc"];
            }
            if (options["influence"]){
                _this.influence = options["influence"];
            }
            if(options["curScenario"]){
                _this.curScenario = options["curScenario"]
            }
            if (options["startTime"]){
                _this.setStartTimeString(options["startTime"]);
            }
            if (options["endTime"]){
                _this.setEndTimeString(options["endTime"]);
            }
        }
    }
  /**
     *
     * @param scenarioScript {ScenarioScript}
     */
  setScenarioScript(scenarioScript){
    let _this =this;
    _this.scenarioScript = scenarioScript;
    }

    getID(){
        return this.actorId;
    }
    getActorId(){
        let _this = this;
        return _this.actorId;
    }
    getActorType(){
        let _this =this;
        return _this.actorType;
    }
    isPrepareValue(){
        let _this =this;
        return _this.isPrepare;
    }
    setStartTimeString(startTimeString){
        let _this =this;
        _this.startTimeString = startTimeString;
        _this.startTimeData = new Date(startTimeString);
        _this.startTimeJulianData = JulianDate.fromDate(_this.startTimeData);
    }
    setEndTimeString(endTimeString){
        let _this =this;
        _this.endTimeString = endTimeString;
        _this.endTimeData = new Date(endTimeString);
        _this.endTimeJulianData = JulianDate.fromDate(_this.endTimeData);
    }
    getStartTimeString(){
        let _this = this;
        return _this.startTimeString;
    }
    getEndTimeString(){
        let _this = this;
        return _this.endTimeString;
    }
    getEndJulianData(){
        let _this = this;
        return _this.endTimeJulianData;
    }
    getStartJulianDate(){
        let _this = this;
        return _this.startTimeJulianData;
    }
    getActorName(){
        let _this = this;
        return _this.actorName;
    }
    prepare () {
        let _this = this;
        //进行绑定
        if (!_this.influence){
            return "没有影响因素！";
        }

        //对时间进行测量
        let start = _this.startTimeJulianData;
        let end = _this.endTimeJulianData;
        // let endTime=_this.scenarioScript.getEndJulianData();

        let entity = _this.influence.getEntity();
        if(!entity){
            entity =  _this.influence.renderRange(_this.curViewer);    
        }
       
        entity.availability = new TimeIntervalCollection([new TimeInterval({
            start : start,
            stop : end
        })]);
        //对于状态的改变进行效果判断
        _this.curViewer.clock.onTick.addEventListener((clock) =>{
            if (_this.isTriggered) {
                return;
            }
          
            let currentTime = clock.currentTime;
            _this.influence.handleInfluence(this.curScenario,currentTime)
            if(JulianDate.lessThan(end,currentTime)){
                entity.show = false;
                _this.isTriggered = true;
            }
        });
        console.log("准备就绪："+_this.actorName);
        _this.isPrepare = true;
    }
    gc(){
        let _this = this;
        _this.isTriggered = false;
        _this.isPrepare = false;
        _this.times = [];
        _this.materials.troop.gc();
        _this.materials.track.gc();
    }
    toJson () {
        let _this =this;

        let materialJson = {
            troop: _this.materials.troop.getTroopId(),
            track:_this.materials.track.getTrackId()
        };
        return{
            actorId:_this.actorId,
            actorName:_this.actorName,
            actorType:_this.actorType,
            startTime:_this.startTimeString,
            endTime:_this.endTimeString,
            actorDesc:_this.actorDescription?_this.actorDescription:"",
            actorMaterials:materialJson,
            scenarioId:scenario_deduce.getCurScenario().getScenarioId()
        }
    }

}


let scenarioScript,deduceEngine;

export default{
    getEngine(v,scg){
      
        scenarioScript = new ScenarioScript(v,scg) 
        deduceEngine = new DeduceEngine(v,scg)
        return this;   
    },
    getScenarioScript(){
        return scenarioScript;
    },
    createActor(actorKey,ops){
       return scenarioScript.createActor(actorKey,ops);
    },
    runScript(){
        deduceEngine.loadScript(scenarioScript);
    }
};